package teacher.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manager.actionform.TeacherForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dao.TeacherMagDao;
import dao.impl.TeacherMagImpl;
import domain.Teacher;

/**
 * 老师修改个人信息的Action
 * 
 * May 26, 2009
 *
 * @author Fabian
 *
 */
public class ManagerSelfInfoAction extends DispatchAction {
	/**
	 *查看老师个人信息
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//获取Session中的Manager的id
		HttpSession session = request.getSession();
		Teacher th = null;
		th = (Teacher)session.getAttribute("user");
		int t_id = th.getId();
		
		TeacherMagDao tm = new TeacherMagImpl();
		th = tm.getTeacher(t_id);
		
		String jsonStr = "";
		if (th != null) {
			//组装返回的json数据
			jsonStr = jsonStr + "{root: [{id: " + + th.getId() + ", name: '" + th.getName() +
			"', sex: '" + th.getSex() + "', nickname: '" + th.getNickname() +
			"', password: '" + th.getPassword() + "', major:'" + th.getMajor() +
			"', major_id: " + th.getMajorId() + "}]}";
		}
		request.setAttribute("result", jsonStr);
		
		return mapping.findForward("show_result");
	}
	/**
	 * 获取老师个人信息，返回，用于修改个人信息
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取Session中的Manager的id
		HttpSession session = request.getSession();
		Teacher th = null;
		th = (Teacher)session.getAttribute("user");
		int t_id = th.getId();
		
		TeacherMagDao tm = new TeacherMagImpl();
		th = tm.getTeacher(t_id);	
		
		//组装返回的JSON数据		
		String jsonStr = "";
		
		if (th != null) {
			jsonStr = jsonStr + "{id: " + th.getId() + ", name: '" + th.getName() +
			"', sex: '" + th.getSex() + "', nickname: '" + th.getNickname() +
			"', password: '" + th.getPassword() + "', major:'" + th.getMajor() +
			"', major_id: " + th.getMajorId() + "}";
		}
		
		request.setAttribute("result", jsonStr);		
		
		return mapping.findForward("get_result");
	}
	
	/**
	 * 修改老师个人信息 
	 */
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HttpSession session = request.getSession();
		Teacher th = null;
		th = (Teacher)session.getAttribute("user");
		
		TeacherForm tf = (TeacherForm)form;
		th.setNickname(tf.getNickname());
		th.setPassword(tf.getPassword());
		
		boolean result = false;
		
		if (th != null) {
			TeacherMagDao tm = new TeacherMagImpl();
			result = tm.modifyTeacher(th);
		}		
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{success: true, msg: '恭喜，个人信息修改成功！请重新登陆！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{success: false, msg: '抱歉，个人信息修改失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}	
		return mapping.findForward("modify_result");
	}
}
